<?php

namespace App\Http\Controllers\Asset;

use App\Exports\AssetInputTemplateExport;
use App\Exports\AssetStockExport;
use App\Exports\AssetStockInputTemplateExport;
use App\Imports\AssetStocksImport;
use Excel;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class AssetStockExcelController extends Controller
{
    public function exportTemplate()
    {
        return Excel::download(new AssetStockInputTemplateExport(), 'asset-stocks.xlsx');
    }

    public function exportExcel()
    {
        return Excel::download(new AssetStockExport(), 'asset_stock_data.xlsx');
    }

    public function import()
    {
        $result = ["error" => false];
        try {
            Excel::import(new AssetStocksImport(), request()->file('upload_file'));
        } catch (\Exception $e) {
            $result = [
                "status" => 'fail',
                "error" => true,
                "message" => $e->getMessage()
            ];
        }
        return response()->json(compact(['result']));
    }
}
